<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="字段类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.dev_form_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="数据字段" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入字段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="页面标签" prop="label">
        <el-input
          v-model="queryParams.label"
          placeholder="请输入标签"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="必填" prop="required">
        <el-select
          v-model="queryParams.required"
          placeholder="请选择必填"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:field:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:field:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:field:import']"
          >导入</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="fieldList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="数据字段" align="center" prop="code" width="180"/>
      <el-table-column label="页面标签" align="center" prop="label" >
        <template slot-scope="scope">
          <el-input v-model="scope.row.label" @change="updateField(scope.row, 'label')"/>
        </template>
      </el-table-column>
      <el-table-column label="字段类型" align="center" prop="type">
        <template slot-scope="scope">
          <el-select v-model="scope.row.type" @change="updateField(scope.row, 'type')">
            <el-option
              v-for="dict in dict.type.dev_form_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>
      
      <el-table-column label="列表" align="center" prop="list">
        <template slot-scope="scope">
          <el-select v-model="scope.row.list" @change="updateField(scope.row, 'list')">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sorts">
        <template slot-scope="scope">
          <el-input type="number" size="mini" v-model="scope.row.sorts" @change="updateField(scope.row, 'sorts')"/>
        </template>
      </el-table-column>

      <el-table-column label="表单模式" align="center" prop="mode">
        <template slot-scope="scope">
          <el-select v-model="scope.row.mode" @change="updateField(scope.row, 'mode')">
            <el-option
              v-for="dict in dict.type.dev_form_mode"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="必填" align="center" prop="required">
        <template slot-scope="scope">
          <el-select v-model="scope.row.required" @change="updateField(scope.row, 'required')">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="数据源" align="center" prop="ds" width="180">
        <template slot-scope="scope">
          <el-input v-model="scope.row.ds" @change="updateField(scope.row, 'ds')"/>
        </template>
      </el-table-column>

      <el-table-column label="默认值" align="center" prop="defaults" >
        <template slot-scope="scope">
          <el-input v-model="scope.row.defaults" @change="updateField(scope.row, 'defaults')"/>
        </template>
      </el-table-column>
      <el-table-column label="搜索类型" align="center" prop="search">
        <template slot-scope="scope">
          <el-select v-model="scope.row.search" @change="updateField(scope.row, 'search')">
            <el-option
              v-for="dict in dict.type.api_sql_exp"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:field:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:field:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pager.page"
      :limit.sync="pager.limit"
      @pagination="getList"
    />
    <!-- 添加或修改模块字段对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字段类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.dev_form_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据字段" prop="code">
          <el-input v-model="form.code" placeholder="请输入字段" />
        </el-form-item>
        <el-form-item label="页面标签" prop="label">
          <el-input v-model="form.label" placeholder="请输入标签" />
        </el-form-item>
        <el-form-item label="必填" prop="required">
          <el-radio-group v-model="form.required">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="数据源" prop="ds">
          <el-input v-model="form.ds" placeholder="请输入数据源" />
        </el-form-item>
        <el-form-item label="表单模式" prop="mode">
          <el-radio-group v-model="form.mode">
            <el-radio
              v-for="dict in dict.type.dev_form_mode"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="默认值" prop="defaults">
          <el-input v-model="form.defaults" placeholder="请输入默认值" />
        </el-form-item>
        <el-form-item label="列表" prop="list">
          <el-radio-group v-model="form.list">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="搜索类型" prop="search">
          <el-select v-model="form.search" placeholder="请选择搜索类型" clear>
            <el-option
              v-for="dict in dict.type.api_sql_exp"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sorts">
          <el-input v-model="form.sorts" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  apiGet,
  apiPost,
  apiPut,
  apiDelete,
  apiDs,
  apiExport,
  apiDownload,
} from "@/api/openapi";
import request from "@/utils/request";

export default {
  name: "Field",
  dicts: ["dev_form_mode", "dev_form_type", "sys_yes_no", "api_sql_exp"],
  data() {
    return {
      moduleId: null,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模块字段表格数据
      fieldList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        module_id: null,
        type: null,
        code: null,
        label: null,
        required: null,
        ds: null,
        mode: null,
        defaults: null,
        list: null,
        search: null,
        sorts: null,
        "@alias": "t",
        "@exp": {},
      },
      pager: {
        page: 1,
        limit: 10,
        order: "sorts asc",
      },
      // 导入参数
      upload: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        module_id: [
          { required: true, message: "应用不能为空", trigger: "blur" },
        ],
        type: [{ required: true, message: "类型不能为空", trigger: "change" }],
        code: [{ required: true, message: "字段不能为空", trigger: "blur" }],
        label: [{ required: true, message: "标签不能为空", trigger: "blur" }],
        required: [
          { required: true, message: "必填不能为空", trigger: "change" },
        ],
        list: [{ required: true, message: "列表不能为空", trigger: "change" }],
        sorts: [{ required: true, message: "排序不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    const moduleId = this.$route.params && this.$route.params.moduleId;
    this.moduleId = moduleId;
    this.queryParams.module_id = moduleId;
    this.getList();
  },
  methods: {
    /** 查询模块字段列表 */
    getList() {
      this.loading = true;
      let params = { "@pager": this.pager };
      params["dev_module_field"] = { ...this.queryParams };
      apiGet(params).then((response) => {
        this.fieldList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        module_id: this.moduleId,
        type: null,
        code: null,
        label: null,
        required: null,
        ds: null,
        mode: null,
        defaults: null,
        list: null,
        search: null,
        form: null,
        sorts: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pager.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模块字段";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      let data = {};
      data["dev_module_field"] = { id: id };
      apiGet(data).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改模块字段";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let data = {
            dev_module_field: this.form,
            "@tags": ["dev_module_field"],
          };
          if (this.form.id != null) {
            apiPut(data).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            apiPost(data).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    updateField(row, field) {
      let data = {"@tags": ["dev_module_field"],"dev_module_field":[{...row}]}
      apiPut(data).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除选中的模块字段数据？")
        .then(function () {
          let data = {};
          data["dev_module_field"] = { id: ids, "@exp": { id: "in" } };
          data["@tags"] = ["dev_module_field"];
          return apiDelete(data);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导入按钮操作 */
    handleImport() {
      request({
        url: "/dev/module/reload/" + this.moduleId,
        method: "get",
      }).then((response) => {
        this.$modal.msgSuccess("导入成功");
        this.getList();
      });
    },
  },
};
</script>
